package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.ScrapOutNotice;
import com.mxpio.erp.common.quality.entity.ScrapOutNoticeLine;
import com.mxpio.erp.common.quality.service.ScrapOutNoticeLineService;
import com.mxpio.erp.common.quality.service.ScrapOutNoticeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ScrapOutNoticeController", description = "报废出库通知单接口")
@RestController
@RequestMapping("/erp/quality/son/")
public class ScrapOutNoticeController {
	
	@Autowired
	private ScrapOutNoticeService scrapOutNoticeService;
	
	@Autowired
	private ScrapOutNoticeLineService scrapOutNoticeLineService;
	

	@GetMapping("list")
	@Operation(summary = "报废出库通知单列表", description = "获取报废出库通知单列表", method = "GET")
	public Result<List<ScrapOutNotice>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ScrapOutNotice> tos = scrapOutNoticeService.list(ScrapOutNotice.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "报废出库通知单列表(分页)", description = "获取报废出库通知单列表(分页)", method = "GET")
	public Result<Page<ScrapOutNotice>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<ScrapOutNotice> page = scrapOutNoticeService.listPage(ScrapOutNotice.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{noticeNo}")
	@Operation(summary = "根据noticeNo获取报废出库通知单", description = "根据noticeNo获取报废出库通知单", method = "GET")
	public Result<ScrapOutNotice> getById(@PathVariable(name = "noticeNo", required = true) String noticeNo) {
		ScrapOutNotice inQualityOrder = scrapOutNoticeService.getById(ScrapOutNotice.class, noticeNo);
		return Result.OK(inQualityOrder);
	}
	
	
	
	@DeleteMapping("remove/{noticeNos}")
	@Operation(summary = "删除报废出库通知单", description = "删除报废出库通知单", method = "DELETE")
	public Result<ScrapOutNotice> remove(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		String noticeNo[] = noticeNos.split(",");
		for(String key : noticeNo){
			Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, key);
			scrapOutNoticeLineService.removeBatch(ScrapOutNoticeLine.class, c);
			scrapOutNoticeService.delete(ScrapOutNotice.class, key);
		}
		return Result.OK();
	}
	
    

	/*@GetMapping("listbynos/{noticeNos}")
	@Operation(summary = "根据noticeNos获取对应报废出库通知单及明细", description = "根据noticeNos获取报废出库通知单及明细", method = "GET")
	public Result<List<ScrapOutNotice>> getByIds(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		return scrapOutNoticeService.listByIds(noticeNos);
	}*/

}
